package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ConnectionBean;
import com.project.bean.UserBean;
import com.project.mapper.IConnectionMapper;
import com.project.service.IConnectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ConnectionServiceImpl implements IConnectionService {
    @Autowired
    private IConnectionMapper connectionMapper;

    @Override
    public IPage<ConnectionBean> findByItem(Integer pageNO, String name, String scope) {
        //根据输入的客户名称，经营的范围动态查询，返回客户分页对象
        QueryWrapper<ConnectionBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("c_name", name);
        }
        if (scope != null && scope.length() != 0) {
            qw.like("c_scope", scope);
        }
        return connectionMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public ConnectionBean findById(Integer id) {//根据id查询客户信息
        return connectionMapper.selectById(id);
    }

    @Override
    public void updateInfo(Integer id, String director, String phone, String scope) {
//根据客户id 修改 客户负责人名字和 电话号码，经营范围
        ConnectionBean connection = connectionMapper.selectById(id);
        connection.setDirector(director);
        connection.setDirectorPhone(phone);
        connection.setScope(scope);
        connectionMapper.updateById(connection);
    }

    @Override
    public boolean add(ConnectionBean connectionBean) {
        //添加客户，如果客户名称相同，则不做添加
        QueryWrapper<ConnectionBean> qw = new QueryWrapper<>();
        String name = connectionBean.getName();
//        System.out.println("名称"+name);
        qw.eq("c_name", name);
        ConnectionBean connection = connectionMapper.selectOne(qw);
//        System.out.println("查出来的"+connection);
        if (connection == null) {
            connectionMapper.insert(connectionBean);
            return true;
        }
        return false;
    }

    @Override
    public List<ConnectionBean> findAll() {
        //查询所有客户
        QueryWrapper<ConnectionBean> qw=new QueryWrapper<>();
        return connectionMapper.selectList(qw);
    }
}
